{extend name="base" /} {block name="body"}
<div class="layui-body">
	<div class="navbar">
		<a><cite>系统设置</cite></a>&nbsp;<span class="layui-box">&gt;</span>
		<a href=""><cite>数据库备份</cite></a>
	</div>
	<!--tab标签-->
	<div class="layui-tab layui-tab-brief">
		<blockquote class="layui-elem-quote">
			<div class="layui-inline">
				<a id="export" href="javascript:;" style="color:#FFFFFF" autocomplete="off">
					<button class="layui-btn">
                        <i class="ace-icon fa fa-database bigger-110"></i> 备份当前数据
                    </button>
				</a>
			</div>
		</blockquote>
		<div class="layui-tab-content">
			<form id="export-form" method="post" class="form-horizontal" action="{:url('export')}">
				<table class="layui-table layui-form">
					<thead>
						<tr class="long-th">
							<th width="4%" class="text-center">
								<input type="checkbox" lay-skin="primary" lay-filter="allChoose">
							</th>
							<th>表名</th>
							<th class="hidden-sm hidden-xs">数据量</th>
							<th class="hidden-sm hidden-xs">数据大小</th>
							<th class="hidden-sm hidden-xs">创建时间</th>
						</tr>
					</thead>
					<tbody>
						{foreach name="data_list" item="v"}
						<tr>
							<td height="28" align="center">
								<input type="checkbox" name="tables[]" value="{$v.name}" class="layui-checkbox checkbox-ids" lay-skin="primary">
							</td>
							<td>{$v.name}</td>
							<td>【{$v.rows}】 条记录 </td>
							<td>{$v.data_length|format_bytes}</td>
							<td>{$v.create_time}</td>
						</tr>
						{/foreach}
					</tbody>
				</table>
			</form>
		</div>
	</div>
</div>
<script>
	// 定义全局JS变量
	var GV = {
		current_controller: "{$controller}/index"
	};
</script>

{/block} {block name="script"}
<script>
	/* 数据库备份、优化、修复 */
	(function($) {
		$("a[id^=optimize_]").click(function() {
			$.get(this.href, function(data) {
				if(data.code == 1) {
					layer.alert(data.msg, {
						icon: 6
					});
				} else {
					layer.alert(data.msg, {
						icon: 5
					});
				}
			});
			return false;
		});
		$("a[id^=repair_]").click(function() {
			$.get(this.href, function(data) {
				if(data.code == 1) {
					layer.alert(data.msg, {
						icon: 6
					});
				} else {
					layer.alert(data.msg, {
						icon: 5
					});
				}
			});
			return false;
		});

		var $form = $("#export-form"),
			$export = $("#export"),
			tables
		$optimize = $("#optimize"), $repair = $("#repair");
		$optimize.add($repair).click(function() {
			var that = this;
			$.post(this.href, $form.serialize(), function(data) {
				if(data.code == 1) {
					layer.alert(data.msg, {
						icon: 6
					}, function(index) {
						layer.close(index);
					});
				} else {
					layer.alert(data.msg, {
						icon: 5
					}, function(index) {
						layer.close(index);
					});
				}
				setTimeout(function() {
					$('#top-alert').find('button').click();
					$(that).removeClass('disabled').prop('disabled', false);
				}, 1500);
			}, "json");
			return false;
		});

		$export.click(function() {
			$export.children().addClass("disabled");
			$export.children().text("正在发送备份请求...");
			var that = this;
			$.post(
				$form.attr("action"),
				$form.serialize(),
				function(data) {
					if(data.code == 1) {
						tables = data.tables;
						$export.children().text(data.msg + "开始备份，请不要关闭本页面！");
						backup(data.tab);
						window.onbeforeunload = function() {
							return "正在备份数据库，请不要关闭！"
						}
					} else {
						layer.alert(data.msg, {
							icon: 5
						});
						$export.children().removeClass("disabled");
						$export.children().text("立即备份");
						setTimeout(function() {
							$('#top-alert').find('button').click();
							$(that).removeClass('disabled').prop('disabled', false);
						}, 1500);
					}
				},
				"json"
			);
			return false;
		});

		function backup(tab, status) {
			status && showmsg(tab.id, "开始备份...(0%)");
			var that = this;
			$.get($form.attr("action"), tab, function(data) {
				if(data.code == 1) {
					showmsg(tab.id, data.msg);
					if(!$.isPlainObject(data.tab)) {
						$export.children().removeClass("disabled");
						$export.children().text("备份完成，点击重新备份");
						window.onbeforeunload = null;
					}
					if(data.tab != undefined) {
						backup(data.tab, tab.id != data.tab.id);
					}
				} else {
					updateAlert(data.msg, 'alert-error');
					$export.children().removeClass("disabled");
					$export.children().text("立即备份");
					setTimeout(function() {
						$('#top-alert').find('button').click();
						$(that).removeClass('disabled').prop('disabled', false);
					}, 1500);
				}
			}, "json");
		}

		function showmsg(id, msg) {
			$tr = $form.find("input[value=" + tables[id] + "]").closest("tr");
			$tr.find(".green").html("");
			$tr.find(".info").html("");
			$tr.find(".backup").html(msg);
		}
	})(jQuery);
</script>
{/block}